package player 
{
	import mx.core.FlexSprite;
	import org.flixel.FlxSprite;
	import org.flixel.FlxPoint;
	import org.flixel.FlxState;
	import org.flixel.FlxText;
	/**
	 * ...
	 * @author João Brant
	 */
	public class Salazar extends FlxSprite
	{
		[Embed(source = "../../assets/playerLegs.png")]
		public static var playerLegsImage:Class;
		[Embed(source = "../../assets/playerHead.png")]
		public static var playerHeadImage:Class;
		[Embed(source = "../../assets/playerBody.png")]
		public static var playerTorsoImage:Class;
		[Embed(source = "../../assets/playerArms.png")]
		public static var playerArmImage:Class;
		[Embed(source = "../../assets/gaiola2.png")]
		public static var gaiola2Image:Class;
		[Embed(source = "../../assets/gaiola.png")]
		public static var gaiolaImage:Class;
		
		private static const TOLERANCIA_RINDO_MUITO:int = 500;
		private var rirMuitoTolerancia:Number = 0;
		private var rindo:Boolean = false;
		
		private var playerLegsSprite:FlxSprite;
		private var playerHeadSprite:FlxSprite;
		private var playerTorsoSprite:FlxSprite;
		private var playerArmSprite:FlxSprite;
		private var gaiolaSprite:FlxSprite;
		private var gaiola2Sprite:FlxSprite;
		private var prisaoTempoText:FlxText;
		
		//Sobre a prisão
		public var prisonCount:Number = 0;
		
		//Sobre a mídia
		public var slowed:Boolean = false;
		public var slowFactor:Number = 3;
		
		public var distanceToFree:Number = 100;
		public var individualityBubble:Number = 10;
		public var popularityDamage:Number = 0.5;
		public var popularityRecover:Number = 0.05;
		
		public var vortexStrength:Number = 3;
		
		private static var MAX_VELOCITY:FlxPoint = new FlxPoint(200, 180);
		
		public var wallPushed:Boolean = false;
		
		public function Salazar(_x:int, _y:int, state:MainState) 
		{
			super(_x, _y);
			makeGraphic(32, 32, 0xffff0000);
			
			playerLegsSprite = new FlxSprite(_x, _y-32);
			playerLegsSprite.loadGraphic(playerLegsImage, true, false, 32, 64);
			playerLegsSprite.addAnimation("walk", [0, 1, 2, 3, 4, 5, 6, 7], 4);
			playerLegsSprite.play("walk");
			
			playerHeadSprite  = new FlxSprite(_x, _y-32);
			playerHeadSprite.loadGraphic(playerHeadImage, true, false, 32, 64);
			playerHeadSprite.addAnimation("walk", [0], -1);
			playerHeadSprite.addAnimation("laugh", [1, 2, 1, 2, 1, 2], 5);
			playerHeadSprite.addAnimation("look", [3], -1);
			playerHeadSprite.play("walk");
			
			playerTorsoSprite = new FlxSprite(_x, _y-32, playerTorsoImage);
			
			playerArmSprite = new FlxSprite(_x, _y);
			playerArmSprite.loadGraphic(playerArmImage, true, false, 32, 64);
			playerArmSprite.addAnimation("walk", [0, 1, 2, 3, 4, 5, 6, 7], 4);
			playerArmSprite.play("walk");
			
			gaiolaSprite = new FlxSprite(-100, -100);
			gaiola2Sprite = new FlxSprite(-100, -100);
			gaiolaSprite.loadGraphic(gaiolaImage);
			gaiola2Sprite.loadGraphic(gaiola2Image);
			
			prisaoTempoText = new FlxText(-100, -100, 20, "Hello");
			prisaoTempoText.setFormat(null, 20);
			prisaoTempoText.color = 0xffff00;
			
			//state.pessoasGroup.add(playerLegsSprite);
			//state.pessoasGroup.add(playerTorsoSprite);
			//state.pessoasGroup.add(playerArmSprite);
			//state.pessoasGroup.add(playerHeadSprite);
		}
		
		public override function draw():void
		{
			//super.draw();
			if (prisonCount > 0)
			{
				gaiola2Sprite.draw();
			}
			playerLegsSprite.draw();
			playerTorsoSprite.draw();
			playerArmSprite.draw();
			playerHeadSprite.draw();
			if (prisonCount > 0)
			{
				gaiolaSprite.draw();
				prisaoTempoText.draw();
			}
		}
		
		public override function postUpdate():void
		{
			super.postUpdate();
			playerLegsSprite.postUpdate();
			playerArmSprite.postUpdate();
			playerTorsoSprite.postUpdate();
			playerHeadSprite.postUpdate();
			gaiolaSprite.postUpdate();
			gaiola2Sprite.postUpdate();
			prisaoTempoText.postUpdate();
		}
		
		public override function update():void
		{
			super.update();
			
			playerLegsSprite.x = x;
			playerLegsSprite.y = y - 32;
			playerArmSprite.x = x;
			playerArmSprite.y = y - 32;
			playerTorsoSprite.y = y - 32;
			playerTorsoSprite.x = x;
			playerHeadSprite.x = x;
			playerHeadSprite.y = y - 32;
			gaiolaSprite.x = x - 16;
			gaiola2Sprite.x = x - 16;
			gaiolaSprite.y = y - 40;
			gaiola2Sprite.y = y - 40;
			if (prisonCount > 0)
			{
				prisaoTempoText.text = new String(new int(prisonCount));
				prisaoTempoText.x = x + 5;
				prisaoTempoText.y = y - 60;
			}
			
			if (rirMuitoTolerancia > TOLERANCIA_RINDO_MUITO && !rindo)
			{
				rindo = true;
				playerHeadSprite.play("laugh");
			}
			else if (rirMuitoTolerancia < TOLERANCIA_RINDO_MUITO && rindo)
			{
				rirMuitoTolerancia = 0;
				playerHeadSprite.play("walk");
				rindo = false;
			}
			if (rirMuitoTolerancia > TOLERANCIA_RINDO_MUITO + 50) rirMuitoTolerancia = TOLERANCIA_RINDO_MUITO + 50;
			rirMuitoTolerancia -= Main.delta * 30;
			if (rirMuitoTolerancia < 0) rirMuitoTolerancia = 0;
			
			
			if (wallPushed)
			{
				playerLegsSprite.play("walk");
				playerArmSprite.play("walk");
					
				playerLegsSprite.animReversed = (scale.x < 0);
				playerArmSprite.animReversed = (scale.x < 0);
					
				playerLegsSprite.getAnimationByName("walk").delay = 0.5;
				playerArmSprite. getAnimationByName("walk").delay = 0.5;
			}
			else
			{
				velocity.x *= 0.9;
				velocity.y *= 0.9;
				
				if (Math.abs(velocity.x) < 30)
					velocity.x = 0;
				
				if (velocity.x == 0)
				{
					playerLegsSprite.getAnimationByName("walk").delay = -1;
					playerArmSprite. getAnimationByName("walk").delay = -1;
					
					playerLegsSprite.frame = 2;
					playerArmSprite.frame = 2;
				}
				else if (velocity.x > 0)
				{
					playerLegsSprite.play("walk");
					playerArmSprite.play("walk");
					
					playerLegsSprite.animReversed = (scale.x < 0);
					playerArmSprite.animReversed = (scale.x < 0);
					
					playerLegsSprite.getAnimationByName("walk").delay = 10 / velocity.x;
					playerArmSprite. getAnimationByName("walk").delay = 10 / velocity.x;
				}
				else if (velocity.x < 0)
				{
					playerLegsSprite.play("walk");
					playerArmSprite.play("walk");
					
					playerLegsSprite.animReversed = (scale.x > 0);
					playerArmSprite.animReversed = (scale.x > 0);
					
					playerLegsSprite.getAnimationByName("walk").delay = - 10 / velocity.x;
					playerArmSprite. getAnimationByName("walk").delay = - 10 / velocity.x;
				}
			}
		}
		
		public function push(x:int, y:int):void
		{
			wallPushed = false;
			
			this.velocity.x += x;
			this.velocity.y += y;
			
			if (!slowed)
			{
				if (this.velocity.x > MAX_VELOCITY.x)  this.velocity.x = MAX_VELOCITY.x;
				else if (-this.velocity.x > MAX_VELOCITY.x) this.velocity.x = -MAX_VELOCITY.x;
				if (this.velocity.y > MAX_VELOCITY.y)  this.velocity.y = MAX_VELOCITY.y;
				else if ( -this.velocity.y > MAX_VELOCITY.y) this.velocity.y = -MAX_VELOCITY.y;
			}
			else
			{
				if (this.velocity.x > MAX_VELOCITY.x/slowFactor)  this.velocity.x = MAX_VELOCITY.x/slowFactor;
				else if (-this.velocity.x > MAX_VELOCITY.x/slowFactor) this.velocity.x = -MAX_VELOCITY.x/slowFactor;
				if (this.velocity.y > MAX_VELOCITY.y/slowFactor)  this.velocity.y = MAX_VELOCITY.y/slowFactor;
				else if ( -this.velocity.y > MAX_VELOCITY.y/slowFactor) this.velocity.y = -MAX_VELOCITY.y/slowFactor;
			}
		}
		
		public function setScale(sx:Number, sy:Number):void
		{
			scale.x = sx;
			playerLegsSprite.scale.x = sx;
			playerArmSprite.scale.x = sx;
			playerHeadSprite.scale.x = sx;
			playerTorsoSprite.scale.x = sx;
			
			scale.y = sy;
			playerLegsSprite.scale.y = sy;
			playerArmSprite.scale.y = sy;
			playerHeadSprite.scale.y = sy;
			playerTorsoSprite.scale.y = sy;
		}
		
		public function aumentarPrazer(n:Number):void
		{
			rirMuitoTolerancia += n;
		}
	}

}